package com.hoshiicloud.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.stock.entity.Warehouse;
import com.hoshiicloud.stock.mapper.IWarehouseMapper;
import com.hoshiicloud.stock.service.IWarehouseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 仓库Service接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/18 13:58
 * @mail : maiboyc@163.com
 **/
@Service
public class WarehouseServiceImpl extends ServiceImpl<IWarehouseMapper, Warehouse> implements IWarehouseService {

    /**
     * 根据id获取
     * @param id ID
     * @return Warehouse
     */
    @Cacheable(cacheNames = "warehouse", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "仓库管理-仓库管理-根据ID查询仓库管理信息")
    @Override
    public Warehouse getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据一组id获取
     * @param ids id组
     * @return List<Warehouse>
     */
    @Cacheable(cacheNames = "warehouse", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#ids")
    @Logable(authName = "仓库管理-仓库管理-根据ID组查询仓库管理信息")
    @Override
    public List<Warehouse> getByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 保存
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "warehouse", allEntries = true)
    @Logable(authName = "仓库管理-仓库管理-新增仓库管理信息")
    @Override
    public Long insert(Warehouse entity) {
        // 自动生成id，创建日期 修改日期 创建人 修改人 删除标识
        entity.setCreatedTime(LocalDateTime.now());
        entity.setCreatedBy("admin");// 需要拿当前的登录人
        entity.setDeleted(0);
        //需要做日期转换
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增仓库管理信息失败"));
        }
        return entity.getId();
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "warehouse", allEntries = true)
    @Logable(authName = "仓库管理-仓库管理-修改仓库管理信息")
    @Override
    public Long update(Warehouse entity) throws ServiceException {
        // 自动填入修改时间 修改人
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setUpdatedBy("admin1");// 需要拿当前的登录人
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改仓库管理信息失败"));
        }
        return entity.getId();
    }

    /**
     * 查询
     * @param entity 查询参数实体
     * @return IPage<Warehouse>
     */
    @Cacheable(cacheNames = "warehouse", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageIndex+':'+#entity.pageSize+':'+#entity.whcode+':'+#entity.province+':'+#entity.provinceName+':'+#entity.city+':'+#entity.cityName+':'+#entity.district+':'+#entity.districtName+':'+#entity.address+':'+#entity.telphone+':'+#entity.charge+':'+#entity.type")
    @Logable(authName = "仓库管理-仓库管理-分页查询仓库管理信息")
    @Override
    public IPage<Warehouse> List(Warehouse entity) {
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper();
        IPage<Warehouse> page = new Page();
        /*
        if (null != entity.getPageIndex()) {
            page.setCurrent(entity.getPageIndex());
        }
        if (null != entity.getPageSize()){
            page.setSize(entity.getPageSize());
        }
        s
         */
        if(StringUtils.isNotEmpty(entity.getWhcode())) {
            queryWrapper.like("whcode", entity.getWhcode());
        }
        if (null != entity.getProvince()){
            queryWrapper.eq("province", entity.getProvince());
        }
        if(StringUtils.isNotEmpty(entity.getProvinceName())) {
            queryWrapper.like("province_name", entity.getProvinceName());
        }
        if (null != entity.getCity()){
            queryWrapper.eq("city", entity.getCity());
        }
        if(StringUtils.isNotEmpty(entity.getCityName())) {
            queryWrapper.like("city_name", entity.getCityName());
        }
        if (null != entity.getDistrict()){
            queryWrapper.eq("district", entity.getDistrict());
        }
        if(StringUtils.isNotEmpty(entity.getDistrictName())) {
            queryWrapper.like("district_name", entity.getDistrictName());
        }
        if(StringUtils.isNotEmpty(entity.getAddress())) {
            queryWrapper.like("address", entity.getAddress());
        }
        if(StringUtils.isNotEmpty(entity.getTelphone())) {
            queryWrapper.like("telephone",entity.getTelphone());
        }
        if(StringUtils.isNotEmpty(entity.getCharge())) {
            queryWrapper.like("charge",entity.getCharge());
        }
        if(null != entity.getType()) {
            queryWrapper.like("type",entity.getType());
        }
        // 默认查询只查询未逻辑删除的数据
        queryWrapper.eq("deleted", 0);
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除
     * @param id ID
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "warehouse", allEntries = true)
    @Logable(authName = "仓库管理-仓库管理-删除仓库管理信息")
    @Override
    public boolean delete(Long id) throws ServiceException {
        Warehouse entity = this.getById(id);
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除仓库管理信息失败"));
        } else {
            return true;
        }
    }

}
